import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";
import userPersonalApi from "@/api-services/apis/system/userPersonalApi";
import { SysUser } from "@/api-services/models/system";

const name = "goodAdmin-user";
export const useUserStore = defineStore({
  id: name,
  state: (): UserState => ({
    accessToken: "",
    userInfo: {}
  }),
  getters: { getAccessToken: state => state.accessToken },
  actions: {
    // Set Token
    setToken(token: string) {
      this.accessToken = token;
    },
    // Set setUserInfo
    setUserInfo(userInfo: SysUser.SysUserInfo) {
      this.userInfo = userInfo;
    },
    async getUserInfo() {
      let res = await userPersonalApi.loginUserDetail();
      this.setUserInfo(res.data.data);
    },
    clearUserStore() {
      localStorage.removeItem(name);
    },
    clearToken() {
      this.accessToken = "";
    }
  },
  persist: piniaPersistConfig(name)
});
